<?
//inculde即可以使用data数据。本页所有的data数据，倾在controller都处理好
include_once("../../../common/module/checkA.php");
include_once("../../../../config.php");

//当前联系人信息
$now_member=select_one_line_all_items($table_Prefix."member","nid = '".$_REQUEST ['nid']."'");
//当前业务员信息
$now_agent=select_one_line_all_items($table_Prefix."employee","nid = '".$now_member ['agentnid']."'");

//新变更业务员信息
$new_agent=select_one_line_all_items($table_Prefix."employee","nid = '".$_REQUEST ['ai_agentnid']."'");


//todo 还要考虑到超级管理员的情况


//鉴定一下当前联系人是否有资格查看
//if($now_member['agentnid']!=$_SESSION['nid'])
//{
//    echo "权限错误 记录到 操作日志";
//    exit;
//}
?>





<?php
//接收需要修改的数据的id
$new_detail_nid          = addslashes($_REQUEST['nid']);


//传递过来的页面信息。返回的时候，会根据这些信息返回原来的页面
$nowpageid = addslashes($_REQUEST['nowpageid']);
$now_extender  = addslashes($_REQUEST['now_extender']);



$_REQUEST['ai_authur']      = $_SESSION['username'];
$_REQUEST['ai_ip']          = $_SERVER["REMOTE_ADDR"];
$_REQUEST['ai_tag']=str_replace("；",";",$_REQUEST['ai_tag']);




//$icon['upfile']      = $_FILES['icon'];
//$icon['path']        = ROOT_PATH_ . "/uploadfiles/";
//
//$iconinfo = upload_file($icon);
//$_REQUEST['ai_icon'] = $iconinfo['name'];
//
////php版本问题导致不能用这种写法。
////$_REQUEST['ai_icon'] = upload_file($icon)['name'];








//检测是否更换了跟踪责任人，是则信息通知

if($now_member['agentnid']!=$_REQUEST['ai_agentnid'])
{

    $url="http://".$host."/api/wechat_message/notice_agent_change.php?openid=".$new_agent['openid'];
    _sock($url);

}



$sql=update_date($table_prefix."member",$_REQUEST,"nid",$new_detail_nid);
$query = mysql_query($sql) or die ("error:".mysql_error());










//这里处理一下跟踪记录部分的内容
if($_REQUEST['member_log_content'])
{

$member_log['ai_authur']      = $_SESSION['username'];
$member_log['ai_ip']          = $_SERVER["REMOTE_ADDR"];
$member_log['ai_name']        = $_REQUEST['member_log_title'];
$member_log['ai_contact_way'] = $_REQUEST['member_log_contact_way'];
$member_log['ai_content']     = $_REQUEST['member_log_content'];
$member_log['ai_excutor']     = $_SESSION['nid'];
$member_log['ai_membernid']   = $_REQUEST['nid']; //这个request 极度容易会在页面中被修改成其他id；目前处理方法就是顶部鉴权
$member_log['ai_unitnid']     = $_REQUEST['unitnid'];//这个request 极度容易会在页面中被修改成其他id；目前处理方法就是顶部鉴权

$sql=insert_date($table_prefix."member_contact_log",$member_log,"nid",$new_detail_nid);

$query = mysql_query($sql) or die ("error:".mysql_error().$sql);







//期限外联系的话，则更新checkpoint。
//在周期内天数（未触及临近期的），则不更新checkpoint




//例如我3号要联系，我四号联系了，那么就更新到次月3号。
//我四号再联系一次，因为未临近次元三号，所以不更新。
//我五号再次联系，因为上次留言是四号，这次留言只差距一天，所以不更新checkpoint。

//为了避免短时间内多次留言，刷新联系时间的情况，不同的checkpoint会有不同的刷新时间点
//每日更新的话，必须在不同一日
//每周更新的话，必须在三日内联系
//每月更新，必须在7天附近
//每年更新，必须在30天附近



    //先判断一下现在的时间是否和checkpoint有特定的差距，看是否需要刷新checkpoint
    //这部分好像没用，就只有echo可以作为输出，看看数据。

//    if( $now_member['period']=='无限制'){$new_checkpoint="";};
//    if(($now_member['period']=='每日') && ( count_days(date('Y-m-d'),$now_member['checkpoint']) < 0 ) ){$new_checkpoint=date("Y-m-d",strtotime("+1 day"  ,strtotime($now_member['checkpoint'])));}
//    if(($now_member['period']=='每周') && ( count_days(date('Y-m-d'),$now_member['checkpoint'])  < 3 ) ){$new_checkpoint=date("Y-m-d",strtotime("+1 week" ,strtotime($now_member['checkpoint'])));}
//    if(($now_member['period']=='每月') && ( count_days(date('Y-m-d'),$now_member['checkpoint'])  < 7 ) ){$new_checkpoint=date("Y-m-d",strtotime("+1 month",strtotime($now_member['checkpoint'])));}
//    if(($now_member['period']=='每年') && ( count_days(date('Y-m-d'),$now_member['checkpoint'])  < 30 ) ){$new_checkpoint=date("Y-m-d",strtotime("+1 year" ,strtotime($now_member['checkpoint'])));}
//
//
    echo "重复周期".$now_member['period'];
    echo "<br/>";

    echo "时间点".$now_member['checkpoint'];
    echo "<br/>";


    echo "当前限期超时".count_days(date('Y-m-d'),$now_member['checkpoint']);
    echo "<br/>";
//
//
//    echo "新时间".$new_checkpoint;
//    echo "<br/>";
//    echo "<br/>";
//    echo "<br/>";




    if(date('Y-m-d')>$now_member['checkpoint'])
        {
            echo "已经超时";
            echo "<br/>";
            echo "<br/>";

            for($i=1;date('Y-m-d')>=$new_checkpoint;$i++)
            {

                if(($now_member['period']=='每日') && ( count_days(date('Y-m-d'),$now_member['checkpoint']) < 0 ) ){$new_checkpoint=date("Y-m-d",strtotime("+$i day"  ,strtotime($now_member['checkpoint'])));}
                if(($now_member['period']=='每周') && ( count_days(date('Y-m-d'),$now_member['checkpoint']) < 3 ) ){$new_checkpoint=date("Y-m-d",strtotime("+$i week" ,strtotime($now_member['checkpoint'])));}
                if(($now_member['period']=='每月') && ( count_days(date('Y-m-d'),$now_member['checkpoint']) < 7 ) ){$new_checkpoint=date("Y-m-d",strtotime("+$i month",strtotime($now_member['checkpoint'])));}
                if(($now_member['period']=='每年') && ( count_days(date('Y-m-d'),$now_member['checkpoint']) < 30 ) ){$new_checkpoint=date("Y-m-d",strtotime("+$i year" ,strtotime($now_member['checkpoint'])));}

                echo "循环内的新时间";
                echo $new_checkpoint;
                echo "<br/>";
                echo "<br/>";
            }

        }
    else
        {
            echo "未超时,必须要在checkpoint以后的几天联系才能更新时间";
            echo count_days(date('Y-m-d'),$now_member['checkpoint']);
            echo "<br/>";

            if(($now_member['period']=='每日') && ( count_days(date('Y-m-d'),$now_member['checkpoint']) == 0 ) ){$new_checkpoint=date("Y-m-d",strtotime("+1 day"  ,strtotime($now_member['checkpoint'])));}
            if(($now_member['period']=='每周') && ( count_days(date('Y-m-d'),$now_member['checkpoint']) < 6 ) ){$new_checkpoint=date("Y-m-d",strtotime("+1 week" ,strtotime($now_member['checkpoint'])));}
            if(($now_member['period']=='每月') && ( count_days(date('Y-m-d'),$now_member['checkpoint']) < 23 ) ){$new_checkpoint=date("Y-m-d",strtotime("+1 month",strtotime($now_member['checkpoint'])));}
            if(($now_member['period']=='每年') && ( count_days(date('Y-m-d'),$now_member['checkpoint']) < 335 ) ){$new_checkpoint=date("Y-m-d",strtotime("+1 year" ,strtotime($now_member['checkpoint'])));}

        }


if($new_checkpoint)
{
    $newcheckpoint['ai_checkpoint']=$new_checkpoint;
    $sql=update_date($table_prefix."member",$newcheckpoint,"nid",$new_detail_nid);
    $query = mysql_query($sql) or die ("error:".mysql_error());
}




}
?>



<script type="text/javascript">
    alert("输入信息已保存，点击确认返回继续操作.");
    document.location.href = "../view/detail-edit.php?nowpageid=<? echo $nowpageid ?>&sortby=<? echo $sortby?>&nid=<? echo $new_detail_nid ?>&now_extender=<? echo $now_extender ?>"
</script>

